#include "BaseSkill.h"
#include "../Cats/CatsManager.h"
#include "../GameManager.h"

using namespace cocos2d;

//===== CONSTRUCTOR =====
BaseSkill::BaseSkill()
{
	_skillDamage = 1;
	_isSkillActivated = false;
	_skillEffectSprite = NULL;
	RECT_BUFFER_RADIUS = 20;
}
BaseSkill::BaseSkill(int skilltype, CCString *skillname, CCString *skilldescription)
{
	_skillType = skilltype;
	_skillName = skillname;
	_skillDescription = skilldescription;
}
//===== DESTRUCTOR ======
BaseSkill::~BaseSkill()
{
}

/*
 * @function: loadSkill
 * @param: filename of the skill button image to load
 * @brief: Create and load the skill as a CCSprite
 * @result: All required data loaded into memory
 */
BaseSkill* BaseSkill::loadSkill(cocos2d::CCString filename)
{
	BaseSkill *skill = new BaseSkill();

	if( skill && skill->initWithFile(filename.getCString()) )
	{
		skill->setColor(ccc3(100,100,100));
		skill->autorelease();
		return skill;
	}

	CC_SAFE_DELETE(skill);
	return NULL;
}

/*
 * @brief: Enable button if player has sufficient charge point to cast skill
 * @param: charge point of the player
 * @result: if sufficient charge point, button will be lit up
 */
void BaseSkill::checkToEnableSkillButton(int currentchargepoints)
{
	if( currentchargepoints >= _chargePointRequired && !_isSkillActivated )
		this->setColor(ccc3(255,255,255));
	else
		this->setColor(ccc3(100,100,100));
}

/*
 * @brief: activate skill for parent class, child class should implement it
 */
void BaseSkill::activateSkill()
{
}

/*
 * @brief: update function for parent class, child class should implement it
 */
void BaseSkill::update(float timeDiff, CatsManager* pCatsRef, int chargepoints)
{
}

/*
 * @brief: Get the customised skill effect rectangle bounding box
 * @return: CCRect with a customised bounding box
 */
CCRect BaseSkill::getSkillEffectRect()
{
	if( !_skillEffectSprite )
		return CCRectMake(0,0,0,0);

	return CCRectMake(_skillEffectSprite->getPosition().x - (_skillEffectSprite->getContentSize().width/2) + RECT_BUFFER_RADIUS,
			_skillEffectSprite->getPosition().y - (_skillEffectSprite->getContentSize().height/2) + RECT_BUFFER_RADIUS,
			_skillEffectSprite->getContentSize().width - RECT_BUFFER_RADIUS *2,
			_skillEffectSprite->getContentSize().height - RECT_BUFFER_RADIUS *2);
}

/*
 * @brief: Touch began funcion
 * @param location: location of the touch
 * @param mygamemanager: reference to GameManager
 * @return: true if this is a skill touch, else false
 */
bool BaseSkill::touchesBegan(cocos2d::CCPoint location, GameManager* mygamemanager)
{
	//Check if sufficient point, if sufficient and is in region, activate
	if( !_isSkillActivated && this->boundingBox().containsPoint(location) && mygamemanager->adjustChargePoint(_chargePointRequired*-1) )
	{
		this->setColor(ccc3(100,100,100));
		activateSkill();

		_isSkillActivated = true;

		return true;
	}

	return false;
}

/*
 * @brief: Touch moved function
 * @param location: location of the touch
 */
void BaseSkill::touchesMoved(cocos2d::CCPoint location)
{
	if( !_isSkillActivated )
		return;
}
